//
//  HYIMGScrollView.m
//  HaiYue
//
//  Created by skyline on 2018/1/31.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYIMGScrollView.h"
@interface HYIMGScrollView()<UIScrollViewDelegate>
{
    CGPoint touchPoint;
}
@property (nonatomic, strong) HYView *view;
@property (nonatomic, strong) HYLabel *label;

@end
@implementation HYIMGScrollView
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.delegate = self;
        self.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor clearColor];
        self.bounces = NO;
        self.pagingEnabled = YES;
    }
    return self;
}

- (HYView *)view
{
    if (!_view)
    {
        _view = [[HYView alloc] initWithColor:[UIColor blackColor]];
        //        _view.alpha = 0.5;
        [self addSubview:_view];
        [_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.top.equalTo(self);
            make.size.mas_equalTo(self.contentSize);
        }];
    }
    return _view;
}

- (HYLabel *)label
{
    if (!_label)
    {
        _label = [[HYLabel alloc] initWithText:@"" textColor:[UIColor whiteColor] textFont:[UIFont systemFontOfSize:ContentFont]];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        window.windowLevel = UIWindowLevelAlert;
        [window addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(window).with.offset(GAP);
            make.centerX.equalTo(window);
        }];
    }
    return _label;
}

- (void)setHidden:(BOOL)hidden
{
    [super setHidden:hidden];
    self.label.hidden = hidden;
}

- (void)setItems:(NSArray *)items
{
    if (_items != items)
    {
        _items = items;
        self.contentSize = CGSizeMake(self.bounds.size.width * _items.count, self.bounds.size.height);
        self.view.hidden = NO;
    }
    [self setupSubIMG];
}

- (void)setCurrentIndex:(NSInteger)currentIndex
{
    if (_currentIndex != currentIndex)
    {
        _currentIndex = currentIndex;
    }
    [self setContentOffset:CGPointMake( _currentIndex * self.bounds.size.width,0)];
    self.label.text = [NSString stringWithFormat:@"%ld/%ld", currentIndex + 1, self.items.count];
}

- (void)setupSubIMG
{
    for (int i = 0; i < _items.count; i ++)
    {
        HYAutoIMG *img  = (HYAutoIMG *)_items[i];
        
        UIImageView *new_img = [[UIImageView alloc]init];
        new_img.backgroundColor = [UIColor greenColor];
        new_img.image = img.image;
        [self addSubview:new_img];
        
        CGFloat times = MIN(self.bounds.size.height /new_img.image.size.height, self.bounds.size.width / new_img.image.size.width);
        [new_img mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).with.offset(SCREEN_WIDTH * i);
            make.centerY.equalTo(self);
            if (img.image)
            {
                make.size.mas_offset(CGSizeMake(new_img.image.size.width * times, new_img.image.size.height * times));
            }
        }];
    }
}

- (void)setLevel:(HYIMGScrollViewLevel)level
{
    if (_level != level)
    {
        _level = level;
        if (self.superview == nil)
        {
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            if (_level == HYIMGScrollViewLevelNormal)
            {
                window.windowLevel = UIWindowLevelNormal;
            }
            else if (_level == HYIMGScrollViewLevelStatus)
            {
                window.windowLevel = UIWindowLevelStatusBar;
            }
            else
            {
                window.windowLevel = UIWindowLevelAlert;
            }
            [window addSubview:self];
            [self mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_offset(UIEdgeInsetsZero);
            }];
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    touchPoint = [touch locationInView:[touch view]];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    CGPoint pointEnd = [touch locationInView:[touch view]];
    
    float x = fabs(pointEnd.x - touchPoint.x);
    float y = fabs(pointEnd.y - touchPoint.y);
    if (x <=10 && y <= 10 )
    {
        self.hidden = YES;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger pages = scrollView.contentOffset.x / self.bounds.size.width;
    self.label.text = [NSString stringWithFormat:@"%ld/%ld", pages + 1, self.items.count];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
